<?php

/**
 * 模型对应数据库表 "product_base".
 *
 * 以下是可用表product_base的栏位:
 * @property integer $id
 * @property string $p_name
 * @property string $p_price
 * @property integer $p_quntity
 * @property double $p_rebate
 * @property integer $p_type
 * @property integer $p_game
 * @property string $p_note
 * @property string $p_img
 */
class ProductBase extends CActiveRecord {

    public $p_pro_array = array(
        '1' => '--产品属性--',
        '2' => '推荐',
        '3' => '特价',
        '4' => '热卖'
    );

    /**
     * 返回指定的基于“ar”的静态模型类.
     * @return ProductBase the static model class
     * @author nirsh
     * @example
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * 返回数据库表的名.
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function tableName() {
        return 'product_base';
    }

    /**
     * 返回验证规则
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function rules() {
        return array(
            array('p_name, p_price, p_game, p_type', 'required'),
            array('p_quntity, p_game, p_status, p_pro', 'numerical', 'integerOnly' => true),
            array('p_rebate', 'numerical'),
            array('p_name', 'length', 'max' => 150),
            array('p_price', 'numerical'),
            array('p_img', 'length', 'max' => 225),
            array('p_note', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, p_name, p_price, p_quntity, p_rebate, p_type, p_game, p_note, p_img, p_status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * 返回关联规则数组。
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * 通过ID获取名称
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function getNameById($id, $nameField) {
        $res = $this->findByPk($id);
        return $res[$nameField] ? $res[$nameField] : '';
    }

    /**
     * 返回属性标签(名称= >标签)数组
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function attributeLabels() {
        return array(
            'id' => '系统编号',
            'p_game' => '游戏',
            'p_type' => '产品类型',
            'p_name' => '产品名称',
            'p_price' => '产品价格($)',
            'p_quntity' => '产品数量',
            'p_rebate' => '折扣',
            'p_pro' => '产品属性',
            'p_note' => '产品说明',
            'p_img' => '产品图片',
            'p_time' => '最后修改时间',
            'p_status' => '产品状态'
        );
    }

    /**
     * 设置查询相关条件
     * @author nirsh
     * @version 5.0
     * @example
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('p_name', $this->p_name, true);
        $criteria->compare('p_price', $this->p_price, true);
        $criteria->compare('p_type', $this->p_type);
        $criteria->compare('p_game', $this->p_game);
        $pageSize = 30;
        if ($this->p_game && $this->p_type)
            $pageSize = 10000;


        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                    'sort' => array(
                        'defaultOrder' => 'p_game ASC, p_name ASC',
                    ),
                    'pagination' => array(
                        'pageSize' => $pageSize
                    ),
                ));
    }

    /**
     * 生成输入框
     * @author flwof
     * @param type $name
     * @param type $id
     * @param type $val 
     */
    public static function createInput($name, $id, $val) {
        //$cTag = $name=='p_price'?"<span style='float:left'>$ </span>":''; 
        echo CHtml::textField("{$name}_{$id}", $val, array('rel-id' => $id, 'rel-field' => $name, 'onchange' => "updateField(this)"));
    }

    public function createStatusImg() {
        if ($this->p_status == 1) {
            $str = '点击下架';
            $img = 'icon_green_on.gif';
            $val = 0;
        } else {
            $str = '点击上架';
            $img = 'icon_red_on.gif';
            $val = 1;
        }

        echo CHtml::image(Yii::app()->theme->baseUrl . "/images/" . $img, $str, array("rel-val" => $val, "rel-id" => $this->id, "rel-field" => "p_status", "onclick" => "updateStatus(this)", "title" => $str, 'style' => 'cursor:pointer;'));
    }

}
